<script setup lang='ts'>
import type { PlusColumn } from 'plus-pro-components'

import { useProductBrandAudit } from '@fl/api/man-api'
import { SubmitBtn } from '@fl/components'

import { StatusOptions } from './constant'

const visible = defineModel<boolean>('visible')

const id = defineModel<string>('id')

const { isPending, mutate } = useProductBrandAudit()

const state = ref({
    reason: '',
    status: '',
})

function handleAddOpen() {
    visible.value = true
}

const columns: PlusColumn[] = [
    {
        label: '审批类型',
        options: StatusOptions,
        prop: 'status',
        valueType: 'select',
    },
    {
        label: '审批意见',
        prop: 'reason',
        valueType: 'textarea',
    },

]

const rules = {
    status: [
        {
            message: '请选择审批状态',
            required: true,
        },
    ],

}
const formRef = ref()

async function submit() {
    const valid = await formRef.value.formInstance.validate()
    if (valid) {
        const params = {
            id: String(id.value),
            ...unref(state),
        }
        mutate(params, {
            onSuccess: () => {
                ElMessage.success('修改成功')
                cancel()
            },
        })
    }
}

function cancel() {
    visible.value = false
}
function reset() {
    formRef.value.formInstance.resetFields()
    id.value = ''
}

defineExpose({
    handleAddOpen,
})
</script>

<template>
    <PlusDialog v-model="visible"
                title="审核"
                width="800"
                close-on-press-escape
                @cancel="cancel"
                @closed="reset"
    >
        <PlusForm ref="formRef"
                  v-model="state"
                  :columns="columns"
                  :label-width="100"
                  :rules="rules"
                  :has-footer="false"
        />

        <template #footer>
            <div class="mx-auto">
                <el-button @click="cancel">
                    取消
                </el-button>

                <SubmitBtn type="primary"
                           :loading="isPending"
                           @click="submit"
                >
                    提交
                </SubmitBtn>
            </div>
        </template>
    </PlusDialog>
</template>
